package org.opengauss.datachecker.extract.adapter;

import org.opengauss.datachecker.common.entry.enums.DataBaseType;
import org.opengauss.datachecker.common.util.SpringUtil;
import org.opengauss.datachecker.extract.adapter.service.CheckRowRule;

import java.util.HashMap;
import java.util.Map;

/**
 * AdapterContext
 * 
 * AdapterContext provides a mapping between database types and their specific implementations of various services.
 * This allows the application to dynamically fetch the correct service implementation based on the database type in use.
 * 
 * @author ：wangchao
 * @date ：Created in 2022/12/2
 * @since ：11
 */
public class AdapterContext {
    private static final Map<DataBaseType, Map<Class<?>, String>> CONTEXT = new HashMap<>();

    static {
        CONTEXT.put(DataBaseType.MS, Map.ofEntries(Map.entry(CheckRowRule.class, "mysqlCheckRowRule")));
        CONTEXT.put(DataBaseType.OG, Map.ofEntries(Map.entry(CheckRowRule.class, "openGaussCheckRowRule")));
        CONTEXT.put(DataBaseType.SS, Map.ofEntries(Map.entry(CheckRowRule.class, "sqlServerCheckRowRule")));
    }

    /**
     * This method retrieves the appropriate bean for the specified database type and service class.
     *
     * @param databaseType the type of database (e.g., MySQL, openGauss, SQL Server)
     * @param classz the class of the service to retrieve
     * @param <T> the type of the service class
     * @return the appropriate bean for the specified database type and service class
     */
    public static <T> T getBean(DataBaseType databaseType, Class<T> classz) {
        final String beanName = CONTEXT.get(databaseType).get(classz);
        return SpringUtil.getBean(beanName, classz);
    }
}
